#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXN 9
// #define MAX_ARRAY_SIZE 10240

int *g_pos;
int *g_start;
int g_count;

void swap ( int* a, int* b ) {
    if (a == b) return;
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

int* Permutation ( int* result, int L[], int Left, int Right ) {
    int i;
    // int* PtrToResult = result;
    if ( Left == Right ) {
        printf("XX %x [ ", g_pos);
        for ( i = 0; i <= Right; i++ ) {
            printf("%d ",L[i]);
            *g_pos = L[i];
            g_pos++;
        }
        // g_pos--;
        g_count++;
        printf(" ] %x \n", g_pos);
        int c = (g_pos- g_start) / ( Right+1);
        if ( c != g_count ){
        printf("\n COUNT: %d %d \n", c, g_count);
         //   printf("\n ERROR: %x %x  %d \n", l0, l1, (l1-l0)/sizeof(int) );
            // free(g_start);
            // exit(-9);
        }
    } else {
        for ( i = Left; i <= Right; i++ ) {
            swap(&L[Left],&L[i]);
            int* cur = Permutation(result,L,Left+1,Right);
            // if (cur == result)
            // result = cur;
            printf("LOOP %x %x \n", cur, result);
            swap(&L[i],&L[Left]);
        }
    }
    return g_pos;
}
int pod(int num){
    int n = 1;
    for (int i = 1; i <= num; i++){
        n *=i;
    }
    printf("POD %d \n", n);
    return n;
}

int main () {
    int N, L[MAXN], i, cnt = 0;
    scanf("%d",&N);
    for ( i = 0; i < N; i++ ) L[i] = i + 1;
    int MAX_ARRAY_SIZE = pod(N) + N + 100;
    int* result = (int*)malloc( MAX_ARRAY_SIZE  *sizeof(int));
    memset(result, 0, MAX_ARRAY_SIZE);
    g_pos = result;
    g_start = result;
    g_count = 0;
    printf("\n INIT: %x \n", result);
    // 
    int* final = Permutation(result,L,0,N-1);
    printf("\nFINAL: %x %d , %x, %d \n", result, (final- result)/N, g_pos, g_count);
    
    int* PtrToResult = result;
    while(PtrToResult) {
        printf("%d ",*PtrToResult);
        PtrToResult++;
        cnt++;
        if ( cnt == N ) {
            printf("\n");
            cnt = 0;
        }
        if (*PtrToResult == 0) break;
    }
    free(result);
    return 0;
}